package com.spring.hot;

import java.util.ArrayList;

/**
 * @author Cizai
 * @since 2022/11/30 21:48
 **/
public class Z字形变换 {

    public String convert(String s, int numRows) {

        if (numRows < 2) {
            return s;
        }
        ArrayList<StringBuilder> list = new ArrayList<>();

        for (int i = 0; i < numRows; i++) {
            list.add(new StringBuilder());
        }

        int flag = -1;
        int index = 0;

        for (int i = 0; i < s.length(); i++) {
            list.get(index).append(s.charAt(i));

            if (index == 0 || index == numRows - 1) {
                flag = -flag;
            }
            index += flag;
        }

        StringBuilder builder = new StringBuilder();
        for (StringBuilder stringBuilder : list) {
            builder.append(stringBuilder);
        }
        return builder.toString();

    }
}
